#!/usr/bin/env ruby

# Copyright (c) 2010-2012 Microsoft Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'optparse'
require './include/config'

@config = MOS::Config.create

@options = { }
optparse = OptionParser.new do |opts|
	opts.banner = "Usage: #{$0} [options]"

	@options[:quiet] = false
	opts.on( '-q', '--quiet', 'Do not print any messages (implies -f)' ) do
		@options[:quiet] = true
	end

	@options[:force] = false
	opts.on( '-f', '--force', 'Do not ask for confirmation' ) do
		@options[:force] = true
	end

	opts.on( '-h', '--help', 'Display this screen' ) do
		puts opts
		exit
	end

	opts.on_tail("")
	opts.on_tail("Reset the local state of this MTurk project, maintaining the configuration files.")
	opts.on_tail("This script does not make any server-side changes.")
end

begin
	optparse.parse!
rescue OptionParser::ParseError => e
	$stderr.puts("Error parsing command-line arguments: " + e.message)
	$stderr.puts("Try '#{$0} --help' for more information.")
	exit
end

if @options[:quiet]
	@options[:force] = true
	devnull = File.exist?('/dev/null') ? '/dev/null' : 'NUL'
	$stdout = File.new(devnull, 'w')
end

if !@options[:force]
	printf("Are you sure you want erase all local HIT/Assignment records? (y/n) ")
	response = STDIN.gets.chomp.downcase
	if response != 'y'
		printf("Exiting...\n")
		exit
	end
end

if File.exist?(@config[:HITSuccessFile])
	File.delete(@config[:HITSuccessFile])
end

if File.exist?(@config[:HITFailureFile])
	File.delete(@config[:HITFailureFile])
end

if File.exist?(@config[:HITResultsFile])
	File.delete(@config[:HITResultsFile])
end

if File.exist?(@config[:HITResultsFile] + '.backup')
	File.delete(@config[:HITResultsFile] + '.backup')
end

if File.exist?('ruby-aws.log')
	File.delete('ruby-aws.log')
end

@config.save

printf("Done.\n")

